package com.admin.system.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.admin.common.constant.CacheNames;
import com.admin.common.core.page.TableDataInfo;
import com.admin.common.core.service.OssService;
import com.admin.common.exception.ServiceException;
import com.admin.common.utils.BeanCopyUtils;
import com.admin.common.utils.StringUtils;
import com.admin.common.utils.file.FileUtils;
import com.admin.common.utils.spring.SpringUtils;
import com.admin.system.domain.SysOss;
import com.admin.system.domain.vo.SysOssQuery;
import com.admin.system.mapper.SysOssMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件上传 服务层实现
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
public class SysOssService implements OssService {

    private final SysOssMapper baseMapper;
    public TableDataInfo<SysOssQuery> queryPageList(SysOssQuery bo) {
        LambdaQueryWrapper<SysOss> lqw = buildQueryWrapper(bo);
        Page<SysOssQuery> result = baseMapper.selectVoPage(bo.getPage().build(), lqw);
        List<SysOssQuery> filterResult = result.getRecords().stream().map(this::matchingUrl).collect(Collectors.toList());
        result.setRecords(filterResult);
        return TableDataInfo.build(result);
    }
    public List<SysOssQuery> listByIds(Collection<Long> ossIds) {
        List<SysOssQuery> list = new ArrayList<>();
        for (Long id : ossIds) {
            SysOssQuery vo = SpringUtils.getAopProxy(this).getById(id);
            if (ObjectUtil.isNotNull(vo)) {
                list.add(this.matchingUrl(vo));
            }
        }
        return list;
    }

    @Override
    public String selectUrlByIds(String ossIds) {
        List<String> list = new ArrayList<>();
        for (Long id : StringUtils.splitTo(ossIds, Convert::toLong)) {
            SysOssQuery vo = SpringUtils.getAopProxy(this).getById(id);
            if (ObjectUtil.isNotNull(vo)) {
                list.add(this.matchingUrl(vo).getUrl());
            }
        }
        return String.join(StringUtils.SEPARATOR, list);
    }

    private LambdaQueryWrapper<SysOss> buildQueryWrapper(SysOssQuery bo) {
        LambdaQueryWrapper<SysOss> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), SysOss::getFileName, bo.getFileName());
        lqw.like(StringUtils.isNotBlank(bo.getOriginalName()), SysOss::getOriginalName, bo.getOriginalName());
        lqw.eq(StringUtils.isNotBlank(bo.getFileSuffix()), SysOss::getFileSuffix, bo.getFileSuffix());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), SysOss::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), SysOss::getCreateBy, bo.getCreateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getService()), SysOss::getService, bo.getService());
        return lqw;
    }

    @Cacheable(cacheNames = CacheNames.SYS_OSS, key = "#ossId")
    public SysOssQuery getById(Long ossId) {
        return baseMapper.selectVoById(ossId);
    }
    public void download(Long ossId, HttpServletResponse response) throws IOException {
        SysOssQuery sysOss = SpringUtils.getAopProxy(this).getById(ossId);
        if (ObjectUtil.isNull(sysOss)) {
            throw new ServiceException("文件数据不存在!");
        }
        FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
        //todo
        //OssClient storage = OssFactory.instance();
        //try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
        //    int available = inputStream.available();
        //    IoUtil.copy(inputStream, response.getOutputStream(), available);
        //    response.setContentLength(available);
        //} catch (Exception e) {
        //    throw new ServiceException(e.getMessage());
        //}
    }
    public SysOssQuery upload(MultipartFile file) {
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        //todo
        //OssClient storage = OssFactory.instance();
        //UploadResult uploadResult;
        //try {
        //    uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        //} catch (IOException e) {
        //    throw new ServiceException(e.getMessage());
        //}
        // 保存文件信息
        SysOss oss = new SysOss();
        //oss.setUrl(uploadResult.getUrl());
        //oss.setFileSuffix(suffix);
        //oss.setFileName(uploadResult.getFilename());
        //oss.setOriginalName(originalfileName);
        //oss.setService(storage.getConfigKey());
        baseMapper.insert(oss);
        SysOssQuery sysOssVo = new SysOssQuery();
        BeanCopyUtils.copy(oss, sysOssVo);
        return this.matchingUrl(sysOssVo);
    }
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 做一些业务上的校验,判断是否需要校验
        }
        List<SysOss> list = baseMapper.selectBatchIds(ids);
        //for (SysOss sysOss : list) {
        //    OssClient storage = OssFactory.instance(sysOss.getService());
        //    storage.delete(sysOss.getUrl());
        //}
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 匹配Url
     *
     * @param oss OSS对象
     * @return oss 匹配Url的OSS对象
     */
    private SysOssQuery matchingUrl(SysOssQuery oss) {
        //OssClient storage = OssFactory.instance(oss.getService());
        //// 仅修改桶类型为 private 的URL，临时URL时长为120s
        //if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) {
        //    oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 120));
        //}
        return oss;
    }
}
